﻿'Nombre:                         AdministracionVentanta
'Descripcion:                    Ventana la cual permite la administracion del catalogo de bancos
'Dependencias:                 
'Fecha de creación:              20090426
'Autor:                          VMORALES
'Fecha de modifcación:         
'Responsable:                  
'Num. de modifcación:            0

Option Explicit On
Option Strict On

#Region "Imports"

Imports System.Windows.Forms
Imports System.ComponentModel

#End Region

Namespace Banco

    ''' <summary>
    ''' Ventana la cual permite la administracion del catalogo de bancos
    ''' </summary>
    Partial Public Class AdministracionVentana

#Region "Constantes"

#End Region

#Region "Miembros"

        Public Event BancoCambiado()
        Private _bancos As BancosUI
        Private Shared provider As Manejador
        Private iecv As IEditableCollectionView
        Private cv As CollectionView
        Private Shared _instancia As AdministracionVentana

#End Region

#Region "Propiedades"

        Public Shared ReadOnly Property Instancia() As AdministracionVentana
            Get
                If _instancia Is Nothing Then
                    _instancia = New AdministracionVentana()
                End If

                Return _instancia
            End Get
        End Property

#End Region

#Region "Contructores"

        Private Sub New()

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

        End Sub

#End Region

#Region "Funciones"

        Private Sub MostrarMensajeError(ByVal handler As Manejador, ByVal e As Exception)
            MessageBox.Show(e.Message)
        End Sub

        Private Sub CargarBancos()
            Dim view As ICollectionView

            If provider Is Nothing Then
                provider = New Manejador()
            End If

            _bancos = provider.ObtenerBancos()
            Me.DataContext = _bancos

            view = CollectionViewSource.GetDefaultView(lstBancos.Items)
            If Not view Is Nothing Then
                iecv = TryCast(view, IEditableCollectionView)
                cv = TryCast(view, CollectionView)
            End If

        End Sub

        Private Sub Editar()
            Dim lbItem As ListBoxItem
            'Validacion de campos

            iecv.EditItem(lstBancos.SelectedItem)

            lbItem = TryCast(lstBancos.ItemContainerGenerator.ContainerFromItem(iecv.CurrentEditItem), ListBoxItem)
            If Not lbItem Is Nothing Then
                lbItem.ContentTemplate = TryCast(Me.FindResource("Edicion"), DataTemplate)
            End If
        End Sub

#End Region

        Private Sub AdministracionVentana_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
            e.Cancel = True
            Me.Hide()
        End Sub

        Private Sub AdministracionVentana_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            CargarBancos()

            AddHandler Manejador.ErrorPersistencia, AddressOf MostrarMensajeError
        End Sub

        Private Sub btnCerrar_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCerrar.Click
            Me.Close()
        End Sub

        Private Sub btnAgregar_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAgregar.Click
            Dim lbItem As ListBoxItem

            If Not iecv.CanAddNew Then
                Return
            End If

            btnAgregar.IsEnabled = False
            iecv.AddNew()
            lstBancos.UpdateLayout()

            lbItem = TryCast(lstBancos.ItemContainerGenerator.ContainerFromItem(iecv.CurrentAddItem), ListBoxItem)
            lbItem.ContentTemplate = TryCast(Me.FindResource("Edicion"), DataTemplate)
            lbItem.Focus()
        End Sub

        Private Sub Editar(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
            Editar()
        End Sub

        Private Sub Cancelar(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
            Dim lbItem As ListBoxItem

            If iecv.IsEditingItem Then
                lbItem = TryCast(lstBancos.ItemContainerGenerator.ContainerFromItem(iecv.CurrentEditItem), ListBoxItem)
                iecv.CancelEdit()
            ElseIf iecv.IsAddingNew Then
                lbItem = TryCast(lstBancos.ItemContainerGenerator.ContainerFromItem(iecv.CurrentAddItem), ListBoxItem)
                iecv.CancelNew()
            End If

            If Not lbItem Is Nothing Then
                lbItem.ContentTemplate = TryCast(Me.FindResource("Default"), DataTemplate)
            End If
        End Sub

        Private Sub Ejecutar(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
            Dim lbItem As ListBoxItem
            Dim tmpBanco As BancoUI

            If iecv.IsEditingItem Then
                tmpBanco = TryCast(iecv.CurrentEditItem, BancoUI)
                lbItem = TryCast(lstBancos.ItemContainerGenerator.ContainerFromItem(iecv.CurrentEditItem), ListBoxItem)
                If tmpBanco.TieneErrores Then
                    iecv.CancelEdit()
                Else
                    iecv.CommitEdit()
                End If
            ElseIf iecv.IsAddingNew Then
                tmpBanco = TryCast(iecv.CurrentAddItem, BancoUI)
                lbItem = TryCast(lstBancos.ItemContainerGenerator.ContainerFromItem(iecv.CurrentAddItem), ListBoxItem)
                If tmpBanco.TieneErrores Then
                    iecv.CancelNew()
                Else
                    iecv.CommitNew()
                End If
                btnAgregar.IsEnabled = True
            End If

            If Not lbItem Is Nothing Then
                lbItem.ContentTemplate = TryCast(Me.FindResource("Default"), DataTemplate)
            End If
        End Sub

        Private Sub Eliminar(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
            If Not lstBancos.SelectedItem Is Nothing Then
                If MessageBox.Show("¿Desea eliminar el banco seleccionado?", "Confirmacion", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) = Forms.DialogResult.Yes Then
                    iecv.Remove(lstBancos.SelectedItem)
                    lstBancos.UpdateLayout()
                End If
            End If
        End Sub

        Private Sub lstBancos_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            Editar()
        End Sub
    End Class

End Namespace